package com.egg.auth.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.auth.entity.Client;
import com.egg.auth.model.request.web.ClientPostOrPut;
import com.egg.auth.model.response.web.ClientInfo;
import com.egg.auth.model.response.web.ClientPage;
import com.egg.auth.service.client.IClientService;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 授权客户端 前端控制器
 * </p>
 *
 * @author east
 * @since 2022-06-13
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "授权客户端")
@RestController
@RequestMapping("/client")
public class WebClientController extends WebController {


    @Autowired
    private IClientService clientService;


    @ApiOperation(value = "授权客户端新增")
    @PostMapping
    public ResponseData<String> clientPost(@RequestBody ClientPostOrPut param) {
        Client client = JsonUtil.objToObj(param, Client.class);
        clientService.baseAdd(client);
        return ResponseData.success(client.getId().toString());
    }

    @ApiOperation(value = "授权客户端分页")
    @GetMapping("/page")
    public ResponseData<PageData<ClientPage>> clientPageGet(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @ApiParam(value = "名称") @RequestParam(value = "name", required = false) String name
    ) {
        LambdaQueryWrapper<Client> qw = clientService.buildLambdaSortQw();
        if (ObjectUtil.isNotEmpty(name)) {
            qw.like(Client::getName, name);
        }
        Page<Client> dbPage = clientService.page(new Page<>(current, pageSize), qw);
        if (ObjectUtil.isEmpty(dbPage.getRecords())) {
            return ResponseData.success(new PageData<>(dbPage));
        }
        List<ClientPage> list = JsonUtil.listToList(dbPage.getRecords(), ClientPage.class);
        return ResponseData.success(new PageData<>(dbPage, list));
    }

    @ApiOperation(value = "授权客户端详情")
    @GetMapping("/{id}/info")
    public ResponseData<ClientInfo> clientInfoGet(
            @ApiParam(value = "授权客户端主键ID", required = true) @PathVariable String id
    ) {
        Client db = clientService.baseGetById(id);
        ClientInfo info = JsonUtil.objToObj(db, ClientInfo.class);
        return ResponseData.success(info);
    }

    @ApiOperation(value = "授权客户端编辑")
    @PutMapping("/{id}")
    public ResponseData<String> clientPut(
            @ApiParam(value = "授权客户端主键ID", required = true) @PathVariable String id
            , @RequestBody ClientPostOrPut edit
    ) {
        Client exist = clientService.baseGetById(id);
        Client info = JsonUtil.objToObj(edit, Client.class);
        info.setId(exist.getId());
        clientService.baseEdit(info);
        return ResponseData.success(exist.getId().toString());
    }

    @ApiOperation(value = "授权客户端删除")
    @DeleteMapping
    public ResponseData<String> clientDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        clientService.baseDel(
                ids.stream().filter(o -> ObjectUtil.isNotEmpty(o)).collect(Collectors.toSet())
        );
        return ResponseData.success();
    }


}

